Find the Missing Number (easy)
We'll cover the following
Problem Statement #
We are given an array containing ‘n’ distinct numbers taken from the range 0 to ‘n’. Since the array has only ‘n’ numbers out of the total ‘n+1’ numbers, find the missing number.
Example 1:
Input: [4, 0, 3, 1]
Output: 2
Example 2:
Input: [8, 3, 5, 2, 4, 6, 0, 1]
Output: 7
Try it yourself #
Try solving this question here:
Solution #
This problem follows the Cyclic Sort pattern. Since the input array contains unique numbers from the range 0 to ‘n’, we can use a similar strategy as discussed in Cyclic Sort to place the numbers on their correct index. Once we have every number in its correct place, we can iterate the array to find the index which does not have the correct number, and that index will be our missing number.
However, there are two differences with Cyclic Sort:
- In this problem, the numbers are ranged from ‘0’ to ‘n’, compared to ‘1’ to ‘n’ in the Cyclic Sort. This will make two changes in our algorithm:
- In this problem, each number should be equal to its index, compared to
index + 1
in the Cyclic Sort. Therefore =>nums[i] == nums[nums[i]]
- Since the array will have ‘n’ numbers, which means array indices will range from 0 to ‘n-1’. Therefore, we will ignore the number ‘n’ as we can’t place it in the array, so =>
nums[i] < nums.length
- In this problem, each number should be equal to its index, compared to
- Say we are at index
i
. If we swap the number at indexi
to place it at the correct index, we can still have the wrong number at indexi
. This was true in Cyclic Sort too. It didn’t cause any problems in Cyclic Sort as over there, we made sure to place one number at its correct place in each step, but that wouldn’t be enough in this problem as we have one extra number due to the larger range. Therefore, we will not move to the next number after the swap until we have a correct number at the indexi
.
Code #
Here is what our algorithm will look like:
Time complexity #
The time complexity of the above algorithm is . In the while
loop, although we are not incrementing the index i
when swapping the numbers, this will result in more than ‘n’ iterations of the loop, but in the worst-case scenario, the while
loop will swap a total of ‘n-1’ numbers and once a number is at its correct index, we will move on to the next number by incrementing i
. In the end, we iterate the input array again to find the first number missing from its index, so overall, our algorithm will take which is asymptotically equivalent to .
Space complexity #
The algorithm runs in constant space .